﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using formautenticacao = BD2_5479_5513.FormAutenticacao;

using memoria_partilhada = BD2_5479_5513.SharedMemory;
using dbcontrol = BD2_5479_5513.DatabaseControl;

namespace BD2_5479_5513
{
    public partial class FormDocente : Form
    {
        public FormDocente()
        {
            InitializeComponent();
            preenchimentoInicial();
        }
        
        public void preenchimentoInicial()
        {
            memoria_partilhada mem = new memoria_partilhada();
            dbcontrol bd = new dbcontrol();
            var cod_utilizador = mem.id_utilizador;
            bd.getDisciplinasLeciona(mem.codUtilizador);
            var lista_leciona = mem.listaleciona;

            foreach (var item in lista_leciona)
            {
                this.comboBox_disciplinas.Items.Add(item[1].ToString() + " - " + item[2].ToString());
            }

            var nome = mem.nome_utilizador;
            this.toolStripStatusLabel4.Text = nome;
        }

        public void preenchimentoAlterarInfo()
        {
            memoria_partilhada mem = new memoria_partilhada();
            dbcontrol bd = new dbcontrol();
            var cod_utilizador = mem.id_utilizador;

        }

        public static void ThreadProcAutenticacao()
        {
            Application.Run(new formautenticacao());
        }

        private void button_terminar_sessao_docente_Click(object sender, EventArgs e)
        {
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProcAutenticacao));
            t.Start();
            this.Close();
        }

        private void button_sair_docente_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProcMarcar));
            t.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            memoria_partilhada mem = new memoria_partilhada();
            dbcontrol bd = new dbcontrol();

            var mensagem = bd.remover_avaliacao(mem.avaliacao_escolhida);

            System.Windows.Forms.MessageBox.Show(mensagem);
        }

        public static void ThreadProcMarcar()
        {
            Application.Run(new FormMarcarAvaliacao());
        }

        public static void ThreadProcVer()
        {
            Application.Run(new FormInscritosAval());
        }

        private void buttonVerInscritos_Click(object sender, EventArgs e)
        {
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProcVer));
            t.Start();
            
        }

        private void escolha_disciplina(object sender, EventArgs e)
        {
            memoria_partilhada mem = new memoria_partilhada();
            dbcontrol bd = new dbcontrol();

            var seleccionado = this.comboBox_disciplinas.SelectedIndex.ToString();

            var texto = this.comboBox_disciplinas.Items[Int32.Parse(seleccionado)].ToString();

            var texto_split = texto.Split(new char[] { ' ' }, 3);

            List<List<String>> lista_leciona = mem.listaleciona;

            var id_disciplina = 0;

            foreach (var item in lista_leciona)
            {
                /*Console.WriteLine("************");
                Console.WriteLine(item[1].ToString());
                Console.WriteLine(texto_split[0]);
                Console.WriteLine(item[2].ToString());
                Console.WriteLine(texto_split[2]);
                Console.WriteLine("************");*/
                if (item[1].ToString() == texto_split[0] && item[2].ToString() == texto_split[2])
                    id_disciplina = Int32.Parse(item[0]);
            }

            mem.id_disciplina = id_disciplina;

            var lista_avaliacoes = bd.getAvaliacoes(id_disciplina);

            var n_avaliacoes = lista_avaliacoes.Count();

            this.Controls.Remove(this.tableLayoutPanel1);

            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();

            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();

            this.tableLayoutPanel1.AutoScroll = true;
            this.tableLayoutPanel1.AutoSize = true;
            this.tableLayoutPanel1.ColumnCount = 6;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 20F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 16.66667F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 11.11111F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 11.11111F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 11.11111F));
            this.tableLayoutPanel1.Controls.Add(this.label2, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.label3, 1, 0);
            this.tableLayoutPanel1.Controls.Add(this.label6, 4, 0);
            this.tableLayoutPanel1.Controls.Add(this.label7, 5, 0);
            this.tableLayoutPanel1.Controls.Add(this.label4, 2, 0);
            this.tableLayoutPanel1.Controls.Add(this.label5, 3, 0);
            this.tableLayoutPanel1.Location = new System.Drawing.Point(13, 80);
            this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 1 + n_avaliacoes;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
            this.tableLayoutPanel1.Size = new System.Drawing.Size(620, 239);
            this.tableLayoutPanel1.TabIndex = 12;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(3, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(28, 13);
            this.label2.TabIndex = 0;
            this.label2.Text = "Tipo";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(188, 0);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(30, 13);
            this.label3.TabIndex = 1;
            this.label3.Text = "Data";
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(482, 0);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(28, 13);
            this.label6.TabIndex = 4;
            this.label6.Text = "Sala";
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(550, 0);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(63, 13);
            this.label7.TabIndex = 5;
            this.label7.Text = "Seleccionar";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(311, 0);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(30, 13);
            this.label4.TabIndex = 2;
            this.label4.Text = "Hora";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(414, 0);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(31, 13);
            this.label5.TabIndex = 3;
            this.label5.Text = "Peso";

            int y = 1;

            foreach (var item in lista_avaliacoes)
            {

                Label label_tipo = new System.Windows.Forms.Label();
                label_tipo.AutoSize = true;
                label_tipo.Text = item[1].ToString();
                this.tableLayoutPanel1.Controls.Add(label_tipo, 0, y);

                Label label_data = new System.Windows.Forms.Label();
                label_data.AutoSize = true;
                var data = item[2].ToString().Split(' ');
                label_data.Text = data[0];
                this.tableLayoutPanel1.Controls.Add(label_data, 1, y);

                Label label_hora = new System.Windows.Forms.Label();
                label_hora.AutoSize = true;
                var horas = item[3].ToString();

                if (horas.Length == 1)
                    horas = '0' + item[3].ToString();

                var minutos = item[4].ToString();

                if (minutos.Length == 1)
                    minutos = '0' + item[4].ToString();

                label_hora.Text = horas + ":" + minutos;
                this.tableLayoutPanel1.Controls.Add(label_hora, 2, y);

                Label label_peso = new System.Windows.Forms.Label();
                label_peso.AutoSize = true;
                label_peso.Text = item[5].ToString();
                this.tableLayoutPanel1.Controls.Add(label_peso, 3, y);

                Label label_sala = new System.Windows.Forms.Label();
                label_sala.AutoSize = true;
                label_sala.Text = item[6].ToString();
                this.tableLayoutPanel1.Controls.Add(label_sala, 4, y);

                RadioButton dynamicRadioButton = new RadioButton();
                dynamicRadioButton.Text = "";
                dynamicRadioButton.Tag = item[0].ToString();
                dynamicRadioButton.Click += new System.EventHandler(this.dynamicRadioButton_CheckedChanged);
                this.tableLayoutPanel1.Controls.Add(dynamicRadioButton, 5, y);

                y++;
               
            }

            this.Controls.Add(this.tableLayoutPanel1);

            //this.toolStripStatusLabel2.Text = id_disciplina.ToString();
        }

        private void dynamicRadioButton_CheckedChanged(Object sender, EventArgs e)
        {
            memoria_partilhada mem = new memoria_partilhada();
            RadioButton btn = sender as RadioButton;

            if (btn != null)
            {
                mem.avaliacao_escolhida =  Int32.Parse(btn.Tag.ToString());
            }
            else
                mem.avaliacao_escolhida = 0;
        }

    }
}
